'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ManageLendCtrl
 * @description
 * # ManageLendCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ManageLendCtrl', function ($scope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        $scope.showcerList = function () {
            // 获取证明列表
            var param = {};
            param.url = openApi.performanceList;
            param.params = {
                id: $scope.param.performanceId
            };
            Util.getPerformanceList(param, function (data) {
                $scope.detailListArr = data.result;
                $scope.param.detailList.forEach(function (item) {
                    $scope.detailListArr.forEach(function (subitem) {
                        if (subitem.uid == item.perDetailId) {
                            subitem.selected = true;
                        }
                    })
                })
            })

        };
        $scope.choosePeople = function () {
            var cerReference = {
                name: "ManageLendCtrl",
                selectTab: "performanceApply_select_project",
                selectTabContent: "投标申请单"
            };
            Util.savaParamForReview($scope.param, id);
            Util.savaDataforsearch(cerReference);

            $location.path("finance/choosepeople")
        };
        $scope.chooseProList = function () {
            $scope.param.detailsStr = ""
            var cerReference = {
                name: "ManageLendCtrl",
                selectTab: "performanceApply_select_performance",
                selectTabContent: "工程名称"
            };
            Util.savaParamForReview($scope.param, id);
            Util.savaDataforsearch(cerReference);

            $location.path("manage/chooseperformance")
        };
        // 过滤availableCount>0的可用材料数目
        $scope.availableCount = function (item) {
            return item.availableCount > 0;
        };
        $scope.toggle = function (item) {
            item.selected = !item.selected;
            $scope.param.detailList = $scope.detailListArr.filter(function (item, index) {
                return item.selected;
            })
            $scope.param.detailsStr = getNames();

        };
        var getNames = function () {
            var arr = [];
            if ($scope.param.detailList.length > 0) {
                $scope.param.detailList.forEach(function (item, index) {
                    arr.push(item.perName || item.dataType)
                })
            }
            return arr.join();

        };
        var getDetails = function () {
            var detailsArr = []
            detailsArr = $scope.param.detailList.map(function (item, index) {
                return {
                    perDetailId: item.perDetailId || item.pkId,
                    perName: item.perName || item.dataType,
                    perCount: 1
                }
            });
            return detailsArr;
        };
        $scope.submit = function (operationId) {
            $scope.param.details = JSON.stringify(getDetails());
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.performanceApply;
            options.param = angular.copy($scope.param, {});
            delete options.param.detailList
            FormSubmit.submit(options,$scope);
        };
        $scope.validate = function () {
            if (!$scope.param.remark) {
                Util.Toast("请填写借业绩事由");
                return;
            }
            if (!$scope.param.returnDate) {
                Util.Toast("请填写预计归还日期");
                return;
            }
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if ($scope.param.detailList.length < 1) {
                Util.Toast("请填选择证明材料");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }

        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                if ($scope.param.performanceId) {
                    $scope.showcerList();
                }
                $scope.param.detailList = $scope.param.detailList || []
            });
        };
        // 初始化
        init();

    });